<template>
  <div class="bg">
    <transition name="form-fade" mode="in-out">
      <section class="form_contianer" v-show="showLogin" v-loading="loginLoading">
        <div class="welcome_tip">欢迎回来</div>
        <div class="manage_tip">昌东镇“三务公开”云上监督平台</div>
        <!-- <div class="manage_tip_english">Aixi Lake Management Office</div> -->
        <el-form :model="loginForm" :rules="rules" ref="loginForm">
          <el-form-item prop="username">
            <el-input v-model.trim="loginForm.username" style="height: 48px" placeholder="用户名"> </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" placeholder="密码" style="height: 48px" show-password
              v-model.trim="loginForm.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="getFindMenu('loginForm')" class="submit_btn">登录</el-button>
          </el-form-item>
        </el-form>
        <p class="tip">温馨提示：请输入正确的账号和密码</p>
      </section>
    </transition>
  </div>
</template>
<script>
import { ElMessage } from 'element-plus'
import { initRouterPath } from '@/router/index.js'
export default {
  data() {
    return {
      showLogin: false,
      loginLoading: false,
      loginForm: {
        username: '',
        password: '',
      },
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
      },
    }
  },

  mounted() {
    this.showLogin = true
    // 绑定监听事件
    window.addEventListener('keydown', this.keyDown)
  },
  unmounted() {
    // 销毁事件
    window.removeEventListener('keydown', this.keyDown, false)
  },
  methods: {
    // 获取token
    // getAccesstoken() {
    //   this.$http.post("/admin/Login/isLogin").then((data) => {
    //     console.log(data);
    //     localStorage.setItem("accesstoken", data.data.access_token);
    //   });
    // },
    // 登录提交
    submitForm(formName) {
      var params = {
        cellphone: this.loginForm.username,
        code: this.loginForm.password,
      }

      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.$http.post('/admin/login/login', params).then((res) => {
            localStorage.setItem('userId', res.data.id)
            this.$store.commit('setNickname', res.data.name)
          })
        } else {
          this.$notify.error({
            title: '错误',
            message: '请输入正确的用户名密码',
            offset: 100,
          })
          return false
        }
      })
    },

    keyDown(e) {
      // 回车则执行登录方法 enter键的ASCII是13
      if (e.keyCode == 13 || e.keyCode == 100) {
        this.getFindMenu() // 定义的登录方法
      }
    },
    // 获取用户菜单信息
    getFindMenu() {
      localStorage.clear()
      sessionStorage.clear()
      this.$store.commit('clearVisitedTags')
      this.$store.commit('setAutoFindMenu', [])
      this.loginLoading = true

      this.$http
        .post('/axh/dtUsers/loginDtUsers', {
          phone: this.loginForm.username,
          password: this.loginForm.password,
        })
        .then((res) => {
          localStorage.setItem('accesstoken', res.token)
          this.$store.commit('setUserInfo', res.dtUsersVo)

          // 查询菜单信息
          this.$http.post('/axh/findMenu').then((findMenuRes) => {
            this.$store.commit('setAutoFindMenu', findMenuRes.result || [])
            initRouterPath()
            this.$router.replace('/')
            ElMessage({
              message: '登陆成功，欢迎您！',
              type: 'success',
            })
          })
        })
        .catch((err) => {
          this.loginLoading = false
        })
    },
  },
}
</script>
<style scoped>
.bg {
  width: 100vw;
  height: 100vh;
  background: url('../../assets/image/loginBackgroud2.png');
  background-repeat: no-repeat;
  background-size: 100% 100%;
  background-position: center;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login_page {
  background-color: #324057;
}

.welcome_tip {
  margin-top: 4%;
  color: #999999;
  font-size: 17px;
  line-height: 22px;
  padding-bottom: 1%;
}

.manage_tip {
  font-size: 26px;
  /* line-height: 45px; */
  color: #35353f;
  padding-bottom: 8%;
}

.manage_tip_english {
  color: #999999;
  font-size: 14px;
  line-height: 22px;
  padding-bottom: 5%;
}

.form_contianer {
  width: 360px;
  height: 338px;
  /* height: auto; */
  padding: 22px 62px 22px 62px;
  border-radius: 5px;
  /* text-align: center; */
  /* background-color: rgba(255, 255, 255, 0.5); */
  background-color: #ffffff;
}

.submit_btn {
  width: 100%;
  height: 40px;
  font-size: 16px;
  background-color: #2c83e4;
  /* background: #f45a8d; */
  border: 0;
}

.tip {
  font-size: 12px;
  color: red;
  text-align: center;
}

.form-fade-enter-active,
.form-fade-leave-active {
  transition: all 1s;
}

.form-fade-enter,
.form-fade-leave-active {
  transform: translate3d(0, -50px, 0);
  opacity: 0;
}
</style>
